<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Models_Mysql_Action extends Modules_Core_Models_Action
{
	public function dbActions($module, $controller)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_controller_action';
		if ($module && $controller) {
			$sql .= ' WHERE module_id = "' . $this->_conn->escape($module) . '"
								AND controller_id = "' . $this->_conn->escape($controller) . '"';
		} 
		$sql .= ' ORDER BY controller_id ASC';
		$rs = $this->_conn->query($sql);
		$actions = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$actions[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $actions;
	}
	
	public function getDbActionsByModule($moduleId)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_controller_action
				WHERE module_id = "' . $this->_conn->escape($moduleId) . '" ORDER BY module_id, controller_id, action_id ASC';
		$rs = $this->_conn->query($sql);
		$actions = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$actions[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $actions;
	}
	
	public function getByMCA($moduleId, $controllerId, $actionId)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_controller_action 
							WHERE module_id = "' . $this->_conn->escape($moduleId) . '"
								AND controller_id = "' . $this->_conn->escape($controllerId) . '"
								AND action_id = "' . $this->_conn->escape($actionId) . '"LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (mysql_num_rows($rs) == 0) ? null : $this->_conn->fetchAll($rs);
		return $return;
	}
	
	public function add($action)
	{
		$this->_conn->insert($action, $this->_conn->_tablePrefix . 'core_controller_action');
	}
	
	public function delete($moduleId, $controllerId, $actionId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . 'core_controller_action 
							WHERE module_id = "' . $this->_conn->escape($moduleId) . '" AND controller_id = "' . $this->_conn->escape($controllerId) . '"
								AND action_id = "' . $this->_conn->escape($actionId) . '"';
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function deleteByController($moduleId, $controllerId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . 'core_controller_action 
							WHERE module_id = "' . $this->_conn->escape($moduleId) . '" AND controller_id = "' . $this->_conn->escape($controllerId) . '"';
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
}